import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/elec_price_title_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

import 'charge_stage_price_widget.dart';

class ChargeStagePriceWidgets extends StatelessWidget {
  final List<dynamic> stagePriceList;
  final Function() addCallBack;
  final Function(int index) deleteCallBack;
  final Function() dateCallBack;
  final Function() typeClickCallBack;
  final bool isCanEdit;
  const ChargeStagePriceWidgets(
      this.stagePriceList, this.addCallBack, this.deleteCallBack,
      this.dateCallBack,this.typeClickCallBack,
      {this.isCanEdit = true,Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      clipBehavior: Clip.hardEdge,
      margin: const EdgeInsets.only(left: 16,right: 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
      ),
        child: Column(
          children: [
            ElecPriceTitleWidget(ResKey.siteSettingElectricityStagePrice.tr),
            ListView.builder(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (BuildContext context, int index) =>
                  ChargeStagePriceWidget(
                    context,
                    '${ResKey.siteSettingElectricityTimeStage.tr}${index + 1}',
                    stagePriceList[index]['intervalStartTime'],
                    stagePriceList[index]['intervalEndTime'],
                    stagePriceList[index]['electricityFees'].toString(),
                    stagePriceList[index]['serviceFees'].toString(),
                        (startDateTime, endDateTime) {
                          if(startDateTime == '23:59'){
                            startDateTime = '24:00';
                          }
                          if(endDateTime == '23:59'){
                            endDateTime = '24:00';
                          }
                      var map = stagePriceList[index];
                      map['intervalStartTime'] = startDateTime;
                      map['intervalEndTime'] = endDateTime;
                      stagePriceList[index] = map;
                     dateCallBack();
                    },
                        () {
                      deleteCallBack(index);
                    },
                        (str) {
                      var map = stagePriceList[index];
                      map['electricityFees'] = str;
                      stagePriceList[index] = map;
                      typeClickCallBack();
                    },(str){
                    var map = stagePriceList[index];
                    map['serviceFees'] = str;
                    stagePriceList[index] = map;
                    typeClickCallBack();
                  },isDelete: true,),
              itemCount: stagePriceList.length,
            ),
            Container(
              height: 0.5,
              color: Colour.dividerColor,
              margin: EdgeInsets.only(left: 16.w, right: 16.w),
            ),
            Visibility(
                visible: isCanEdit,
                child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  addCallBack();
                },
                child: SizedBox(
                  height: 49,
                  child: Row(
                    children: [
                      Container(
                        padding: EdgeInsets.only(left: 16, right: 16),
                        child: Image.asset(
                          'assets/images/site_elec_price_add.png',
                          height: 18,
                          width: 18,
                        ),
                      ),
                      Text(
                        ResKey.siteSettingElectricityAddStage.tr,
                        style: ytTextStyle(
                            fontWeight: FontWeight.normal,
                            color: Colour.primaryColor,
                            fontSize: 14),
                      ),
                    ],
                  ),
                )))
          ],
        ));
  }

}
